package org.letva.toink.persistence;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.letva.toink.common.ToinkException;

import com.google.inject.Inject;

public class PersistenceManager {

	@Inject
	private SessionFactory sessionFactory;

	public void commitTransaction() throws ToinkException {
		Session session = sessionFactory.getCurrentSession();
		try {
			if (session.isOpen()) {
				if (session.getTransaction().isActive()) {
					session.getTransaction().commit();
				} else {
					session.close();
				}
			}
		} catch (HibernateException e) {
			throw new ToinkException("Commit transaction failed", e);
		}
	}
}
